package com.jacob.androidframe.controls;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.LinearLayout;

public class LinearLayoutCheckable extends LinearLayout implements Checkable {
	private boolean mChecked = false;
	private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };

	public LinearLayoutCheckable(Context pContext) {
		super(pContext);
	}

	public LinearLayoutCheckable(Context pContext, AttributeSet pAttrs) {
		super(pContext, pAttrs);
	}

	@Override
	protected int[] onCreateDrawableState(int extraSpace) {
		final int[] _DrawableState = super
				.onCreateDrawableState(extraSpace + 1);
		if (isChecked())
			mergeDrawableStates(_DrawableState, CHECKED_STATE_SET);
		return _DrawableState;
	}
	
//	@Override
//	protected void drawableStateChanged() {
//		super.drawableStateChanged();
//		final Drawable drawable = getBackground();
//		if (drawable != null) {
//			final int[] myDrawableState = getDrawableState();
//			drawable.setState(myDrawableState);
//			invalidate();
//		}
//	}

	@Override
	public boolean isChecked() {
		return mChecked;
	}

	@Override
	public void setChecked(boolean pChecked) {
		if (mChecked != pChecked) {
            mChecked = pChecked;
            refreshDrawableState();
        }
	}

	@Override
	public void toggle() {
		setChecked(!mChecked);
	}
}
